var __extends =
    (this && this.__extends) ||
    (function () {
      var e = function (t, s) {
        return (e =
          Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array &&
            function (e, t) {
              e.__proto__ = t;
            }) ||
          function (e, t) {
            for (var s in t)
              Object.prototype.hasOwnProperty.call(t, s) && (e[s] = t[s]);
          })(t, s);
      };
      return function (t, s) {
        function o() {
          this.constructor = t;
        }
        if ("function" != typeof s && null !== s)
          throw new TypeError(
            "Class extends value " + String(s) + " is not a constructor or null"
          );
        e(t, s),
          (t.prototype =
            null === s
              ? Object.create(s)
              : ((o.prototype = s.prototype), new o()));
      };
    })(),
  Utils = common.Utils,
  stageWidth = Browser.clientWidth,
  stageHeight = Browser.clientHeight,
  swipeThreshold = 100,
  SelectRoleView = (function (e) {
    function t() {
      var t = e.call(this) || this;
      return (
        (t.selRoleIndex = -1),
        (t.dataSource = []),
        (t.rolesArray = []),
        (t.rolesSexArr = []),
        (t.trackStep = Laya.stage.width),
        (t.TOGGLE_DIST = t.trackStep / 2),
        (t.isOldeVersion = !0),
        t.btn_selPreBack.on(Laya.Event.CLICK, t, t.onPreBackFn),
        t.btn_newRole.on(Laya.Event.CLICK, t, t.OnSetNewRoleFn),
        t.btn_startGames.on(Laya.Event.CLICK, t, t.onLoadGameMainFn),
        Laya.init(stageWidth, stageHeight),
        t.on(Laya.Event.MOUSE_DOWN, t, t.onTouchStart),
        t.on(Laya.Event.MOUSE_UP, t, t.onTouchEnd),
        t.btn_hero_pre.on(Laya.Event.CLICK, t, t.onMouseDown),
        t.btn_hero_next.on(Laya.Event.CLICK, t, t.onMouseDown),
        (t.beiginPos = 0),
        (t.endPos = t.beiginPos + t.trackStep),
        t.btn_roleManager.on(Laya.Event.CLICK, t, t.onRoleManagerFn),
        WebSocketService.addListener({
          caller: t,
          code: 3,
          method: t.loadRoleDataMsgFn,
        }),
        t.btn_select_version_jingdian.on(
          Laya.Event.CLICK,
          t,
          t.onDidSelectVersion
        ),
        t.btn_select_version_xinchao.on(
          Laya.Event.CLICK,
          t,
          t.onDidSelectVersion
        ),
        t
      );
    }
    return (
      __extends(t, e),
      (t.open = function (e) {
        if (
          (void 0 === e && (e = !0),
          null == this.instance && (this.instance = new t()),
          this.instance.initRequestData(),
          (this.instance.btn_startGames.visible = !1),
          Laya.stage.addChild(this.instance),
          (this.instance.selRoleIndex = 1),
          (this.instance.isOldeVersion = !0),
          (this.instance.preSelectButton =
            this.instance.btn_select_version_jingdian),
          (this.instance.btn_select_version_jingdian.skin =
            "comp/preload/btn_com_sure.png"),
          (this.instance.btn_select_version_xinchao.skin =
            "comp/commonpic/btn_dialog_silver.png"),
          !e)
        ) {
          var s = Laya.stage.getChildByName("GameMainView");
          null != s && s.removeSelf();
        }
      }),
      (t.prototype.onRoleManagerFn = function () {
        var e = RoleManageView.openRoleManagerView(this.roleObject);
        e.pos(0, 0), this.addChild(e);
      }),
      (t.prototype.initRequestData = function () {
        (this.leftrole.skin = ""),
          (this.currentrole.skin = ""),
          (this.rightrole.skin = ""),
          (this.roleName.text = ""),
          (this.id_lbl.text = ""),
          (this.level_lbl.text = ""),
          (this.sex_lbl.text = ""),
          (this.profess_lbl.text = ""),
          WebSocketService.send({ c: 3, d: null });
      }),
      (t.prototype.onMouseDown = function (e) {
        "item_back" == e.target.name ||
          "item_back_rm" == e.target.name ||
          "" == e.target.name ||
          (Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onMouseMove),
          void 0 != this.currentrole && (this.currentPos = this.currentrole.x),
          Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onMouseUp),
          Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.onMouseOut));
      }),
      (t.prototype.onMouseMove = function (e) {
        if ("item_back" == e.target.name || "item_back_rm" == e.target.name);
        else Math.max(Math.min(Laya.stage.mouseX, this.endPos), this.beiginPos);
      }),
      (t.prototype.onMouseOut = function (e) {}),
      (t.prototype.onMouseUp = function (e) {
        var t = e.target.name;
        if ("item_jingdian" !== t && "item_xinchao" !== t) {
          Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMouseMove),
            Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onMouseUp),
            Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.onMouseDown);
          var s = this.dataSource.length,
            o = Math.max(
              Math.min(Laya.stage.mouseX, this.endPos),
              this.beiginPos
            ),
            a = Laya.stage.width / 3,
            n = (2 * Laya.stage.width) / 3;
          o < a
            ? this.selRoleIndex >= s
              ? ((this.selRoleIndex = s),
                ToastView.toastView("没有了，往右滑试试"))
              : this.selRoleIndex++
            : o > n &&
              (this.selRoleIndex <= 1
                ? ((this.selRoleIndex = 1),
                  ToastView.toastView("没有了，往左滑试试"))
                : this.selRoleIndex--),
            this.changeRoleSkinFn(s);
        }
      }),
      (t.prototype.onDidClickChangeHero = function (e) {
        var t = this.dataSource.length;
        "item_pre" == e.target.name
          ? this.selRoleIndex >= t
            ? ((this.selRoleIndex = t),
              ToastView.toastView("没有了，往右滑试试"))
            : this.selRoleIndex++
          : "item_next" == e.target.name &&
            (this.selRoleIndex <= 1
              ? ((this.selRoleIndex = 1),
                ToastView.toastView("没有了，往左滑试试"))
              : this.selRoleIndex--),
          this.changeRoleSkinFn(t);
      }),
      (t.prototype.changeRoleSkinFn = function (e) {
        var t,
          s,
          o = this.selRoleIndex - 1;
        if (!(this.selRoleIndex <= 0 || this.dataSource.length <= 0)) {
          this.selRoleIndex >= this.dataSource.length &&
            (this.selRoleIndex = this.dataSource.length),
            (s = this.dealRoateRole(o + 1)),
            (t = this.dealRoateRole(o - 1));
          var a, n, i;
          (n = this.rolesArray[t]),
            (a = this.rolesArray[o]),
            (i = this.rolesArray[s]),
            this.showCurrentHeroInfo();
          var r, l, h;
          (l = this.rolesSexArr[t]),
            (r = this.rolesSexArr[o]),
            (h = this.rolesSexArr[s]);
          var c = "comp/hreoImg/ws" + this.setRoleImage(n, l) + "_m.png",
            d = "comp/hreoImg/ws" + this.setRoleImage(a, r) + "_m.png",
            p = "comp/hreoImg/ws" + this.setRoleImage(i, h) + "_m.png";
          this.setRoleImage(n, l) <= 0 && (c = ""),
            this.setRoleImage(a, r) <= 0 && (d = ""),
            this.setRoleImage(i, h) <= 0 && (p = ""),
            (this.leftrole.skin = c),
            (this.currentrole.skin = d),
            (this.rightrole.skin = p),
            this.dealCanStartGame();
        }
      }),
      (t.prototype.dealCanStartGame = function () {
        var e = this.roleObject[this.selRoleIndex - 1];
        6 == e.allowLogin || 7 == e.allowLogin
          ? (this.btn_startGames.disabled = !0)
          : (this.btn_startGames.disabled = !1);
      }),
      (t.prototype.showCurrentHeroInfo = function () {
        var e = JSON.stringify(this.dataSource[this.selRoleIndex - 1]),
          t = JSON.parse(e);
        (this.roleName.text = t.roleName),
          (this.roleName.width = 15 * t.roleName.length),
          (this.id_lbl.x = this.roleName.x + this.roleName.width),
          (this.id_lbl.text = "（" + t.id_lbl + "）"),
          (this.sex_lbl.text = t.sex_lbl),
          (this.level_lbl.text = t.level_lbl),
          (this.profess_lbl.text = t.profess_lbl);
      }),
      (t.prototype.setRoleImage = function (e, t) {
        return "文人" == e
          ? "女" == t
            ? 5
            : 2
          : "武士" == e
          ? "女" == t
            ? 4
            : 1
          : "异师" == e
          ? "女" == t
            ? 6
            : 3
          : 0;
      }),
      (t.prototype.loadRoleDataMsgFn = function (e) {
        this.btn_startGames.visible = !0;
        var t = e.roleList;
        if (
          ((this.roleObject = e.roleList),
          (this.rolesArray = []),
          (this.rolesSexArr = []),
          (this.dataSource = []),
          t.length >= 3
            ? (this.btn_newRole.visible = !1)
            : (this.btn_newRole.visible = !0),
          t.length <= 0)
        )
          return (
            (this.selRoleIndex = -1), void (this.btn_startGames.visible = !1)
          );
        this.btn_startGames.visible = !0;
        for (var s, o, a, n = 0; n < t.length; n++) {
          (o = t[n].roleType), (s = t[n].sex);
          var i = Utils.dealRole_Type(o),
            r = Utils.transform(s, "roleSexType");
          (a = {
            roleName: t[n].name,
            roleIcon: "",
            id_lbl: t[n].id,
            level_lbl: Utils.dealTransAndLvl(o, t[n].lvl),
            sex_lbl: r,
            profess_lbl: i,
          }),
            this.rolesArray.push(i),
            this.rolesSexArr.push(r),
            this.dataSource.push(a);
        }
        var l = Laya.LocalStorage.getItem("default_Index");
        if (l) {
          var h = JSON.parse(l),
            c = laya.net.LocalStorage.getItem("mobileText");
          h.userAcc == c && (this.selRoleIndex = h.def_Index);
        }
        this.changeRoleSkinFn(this.dataSource.length);
      }),
      (t.prototype.OnSetNewRoleFn = function () {
        var e = this,
          t = [
            { url: "res/atlas/comp/createRole.atlas", type: Laya.Loader.ATLAS },
          ];
        Laya.loader.load(
          t,
          Laya.Handler.create(this, function () {
            e.addChild(SetNewRole.openSetNewRole());
          }),
          Laya.Handler.create(
            AXProgressBarView,
            AXProgressBarView.onLoadedRecourse,
            null,
            !1
          )
        );
      }),
      (t.prototype.onLoadGameMainFn = function (e) {
        if (-1 != this.selRoleIndex) {
          var t = JSON.stringify(this.dataSource[this.selRoleIndex - 1]),
            s = JSON.parse(t);
          this.selRoleID = parseInt(s.id_lbl);
          var o = this.roleObject[this.selRoleIndex - 1];
          Global.roleLvl = o.lvl;
          var a = {
              userAcc: laya.net.LocalStorage.getItem("mobileText"),
              def_Index: this.selRoleIndex,
            },
            n = JSON.stringify(a);
          Laya.LocalStorage.setItem("default_Index", n),
            (Laya.loader.retryNum = 2);
          var i = [
            { url: "res/atlas/comp.atlas", type: Laya.Loader.ATLAS },
            { url: "res/atlas/common.atlas", type: Laya.Loader.ATLAS },
            { url: "res/atlas/comp/happy.atlas", type: Laya.Loader.ATLAS },
            { url: "res/atlas/emoji.atlas", type: Laya.Loader.ATLAS },
            { url: "res/atlas/stone.atlas", type: Laya.Loader.ATLAS },
          ];
          Laya.loader.load(
            i,
            Laya.Handler.create(this, this.OnLoadedRecourse),
            Laya.Handler.create(
              AXProgressBarView,
              AXProgressBarView.onLoadedRecourse,
              null,
              !1
            )
          );
        } else ToastView.toastView("请选择角色");
      }),
      (t.prototype.OnLoadedRecourse = function (e) {
        this.removeSelf();
        var t = new GameMainView(this.selRoleID, this.isOldeVersion);
        (Global.roleId = this.selRoleID), Laya.stage.addChild(t), t.reset();
      }),
      (t.prototype.showProgressBar = function () {
        (this.progressBar = new ProgressBar()),
          (this.progressBar.x = 10),
          (this.progressBar.y = 200),
          (this.progressBar.value = 0),
          (this.progressBar.width = 355),
          (this.progressBar.height = 20),
          (this.progressBar.sizeGrid = "5,10,5,10"),
          (this.progressBar.changeHandler = new Handler(
            this,
            this.onProgressBarChange
          ));
      }),
      (t.prototype.onProgressBarChange = function () {}),
      (t.prototype.onPreBackFn = function () {
        WebSocketService.close(), this.removeSelf();
        var e = ServerListView.openServerListView();
        Laya.stage.addChild(e);
      }),
      (t.prototype.dealRoateRole = function (e) {
        return e;
      }),
      (t.prototype.onDidSelectVersion = function (e) {
        this.preSelectButton &&
          (this.preSelectButton.skin = "comp/commonpic/btn_dialog_silver.png"),
          (e.target.skin = "comp/preload/btn_com_sure.png"),
          (this.preSelectButton = e.target),
          "item_jingdian" == e.target.name
            ? (this.isOldeVersion = !0)
            : "item_xinchao" == e.target.name && (this.isOldeVersion = !1);
      }),
      (t.prototype.onTouchStart = function (e) {
        this.startTouchX = e.stageX;
      }),
      (t.prototype.onTouchEnd = function (e) {
        (this.endTouchX = e.stageX), this.handleSwipe();
      }),
      (t.prototype.handleSwipe = function () {
        var e = this.endTouchX - this.startTouchX,
          t = this.dataSource.length;
        Math.abs(e) >= swipeThreshold &&
          (e > 0
            ? (console.log("向右滑动"),
              this.selRoleIndex <= 1
                ? ((this.selRoleIndex = 1),
                  ToastView.toastView("没有了，往左滑试试"))
                : this.selRoleIndex--)
            : (console.log("向左滑动"),
              this.selRoleIndex >= t
                ? ((this.selRoleIndex = t),
                  ToastView.toastView("没有了，往右滑试试"))
                : this.selRoleIndex++),
          this.changeRoleSkinFn(t));
      }),
      (t.prototype.close = function () {
        this.off(Laya.Event.MOUSE_DOWN, this, this.onTouchStart),
          this.off(Laya.Event.MOUSE_UP, this, this.onTouchEnd);
      }),
      (t.instance = null),
      t
    );
  })(ui.SelectRoleUI);
